﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Imports AutoDevModel
Partial Class Control_Users_UserProfile
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            hidID.Value = Session("UserId")
            ShowData()

        End If
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities

            Dim p As User = (From q In data.Users Where q.UserId = hidID.Value).SingleOrDefault
            If Not p Is Nothing AndAlso p.UserId = Session("UserId") Then
                txtUsername.Text = p.UserName
                txtUsername.Enabled = False
                txtPhone.Text = p.Phone
                txtDiaChi.Text = p.DiaChi
                txtEmail.Text = p.Email
                curUserName.Value = p.UserName
                txtPassword.Attributes.Add("value", p.Password)
                txtFullname.Text = IIf(IsNothing(p.FullName) = True, "", p.FullName)
                Dim _curHuyen As Integer = Session("HuyenId")

                If Session("IsUser") = LoaiNguoiDung.TCKH Then
                    divAccMCC.Visible = True
                    Using _da As New AutoDevEntities
                        Dim k = (From q In _da.Accs Where q.WebsiteID = _curHuyen Select q).FirstOrDefault
                        If Not k Is Nothing Then
                            txtUserNameMCC.Text = k.AccName
                            txtMccPass.Attributes.Add("value", k.Password)
                            txtFullNameMCC.Text = k.Sport 'for fullname
                        End If
                    End Using

                Else
                    divAccMCC.Visible = False
                End If

            End If
        End Using
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If checkExistUsername() = False Then
            Using data As New VSEntities
                Dim p As User = (From q In data.Users Where q.UserId = hidID.Value).SingleOrDefault
                Try
                    'update useraccount
                    ' p.UserName = hidID.Value
                    p.Phone = txtPhone.Text.Trim()
                    p.DiaChi = txtDiaChi.Text.Trim()
                    p.Email = txtEmail.Text.Trim()
                    p.FullName = txtFullname.Text.Trim()
                    data.SaveChanges()
                    'get list role of user in userrole table
                    Dim _IdUserName = (From q In data.Users Where q.UserName = hidID.Value Select q.UserId).SingleOrDefault
                    Dim ur = (From q In data.UserRoles Where q.UserId = _IdUserName Select q.RoleId).ToArray
                    Dim _curHuyen As Integer = Session("HuyenId")

                    If Session("IsUser") = LoaiNguoiDung.TCKH Then
                        Using _da As New AutoDevEntities
                            Dim k = (From q In _da.Accs Where q.WebsiteID = _curHuyen Select q).FirstOrDefault
                            If Not k Is Nothing Then
                                k.AccName = txtUserNameMCC.Text
                                k.Password = txtMccPass.Text
                                k.Sport = txtFullNameMCC.Text  'for fullname
                                _da.SaveChanges()
                            End If
                        End Using
                    End If

                    ' Insert_App_Log("Edit User:" & txtUsername.Text.Trim() & "", Function_Name.User, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Logged.aspx';", Me.Page, True)

                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Cập nhật thất bại.')';", Me.Page, True)
                End Try
            End Using

        Else

            Excute_Javascript("Alertbox('Tên đăng nhập đã tồn tại trong hệ thống.');", Me.Page, True)
        End If


    End Sub

    Protected Function checkExistUsername() As Boolean
        If txtUsername.Text.Trim() = curUserName.Value Then
            Return False
        End If

        Using data As New VSEntities
            Dim k As User = (From q In data.Users Where q.UserName = txtUsername.Text.Trim()).SingleOrDefault
            If Not k Is Nothing Then
                Return True
            End If
        End Using

        Return False
    End Function

#End Region
End Class
